<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="100px" size="medium" class="ry_form">
      <el-form-item label="装备分类" prop="categoryId">
        <el-select v-model="queryParams.categoryId" placeholder="请选择装备分类" clearable size="small">
              <el-option
              v-for="dict in equipCategory"
              :key="dict.categoryId"
              :label="dict.name"
              :value="dict.categoryId" />
        </el-select>
      </el-form-item>
      <el-form-item label="名称" prop="name">
        <el-input
          v-model.trim="queryParams.name"
          placeholder="请输入名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="详情" prop="detail">
        <el-input
          v-model.trim="queryParams.detail"
          placeholder="请输入详情"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item class="flex_one tr">
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['info:equip:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['info:equip:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['info:equip:remove']"
        >删除</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['info:equip:export']"
        >导出</el-button>
      </el-col> -->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" :columns="columns"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="equipList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="名称" align="center" prop="name" v-if="columns[2].visible"/>
      <el-table-column label="装备分类" align="center" prop="categoryId">
        <template slot-scope="scope">
           <span>{{ getName(scope.row.categoryId) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="图片" align="center" prop="img" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.img" :width="50" :height="50"></image-preview>
        </template>
      </el-table-column>
      <el-table-column label="品牌" align="center" prop="brandName"/>
      <el-table-column label="钓鱼场景" align="center" prop="fishingType"/>
      <el-table-column label="装备类型" align="center" prop="equipType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.equop_status" :value="scope.row.equipType" />
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['info:equip:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['info:equip:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改装备管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="50%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="108px" inline class="dialog-form-two">
        <el-form-item label="名称" prop="name">
          <el-input v-model.trim="form.name" placeholder="请输入名称" />
        </el-form-item>
        <el-form-item label="装备分类" prop="categoryId">
          <el-select v-model="form.categoryId" placeholder="请选择装备分类">
            <el-option
            v-for="dict in equipCategory"
            :key="dict.categoryId"
            :label="dict.name"
            :value="dict.categoryId" />
          </el-select>
        </el-form-item>
        <el-form-item label="品牌" prop="brand">
          <brand-select v-model="form.brand" @change="onBrandChange"></brand-select>
        </el-form-item>

        <el-form-item label="钓鱼场景" prop="equipLists">
          <el-select multiple v-model="equipLists" placeholder="请选择钓鱼场景">
            <el-option
              v-for="item in equipOptions"
              :key = "item.value"
              :label="item.label"
              :value="item.value"/>
          </el-select>
        </el-form-item>
        <el-form-item label="装备类型" prop="equipType">
          <el-select v-model="form.equipType" placeholder="请选择装备类型" clearable>
            <el-option v-for="dict in dict.type.equop_status" :key="dict.value" :label="dict.label"
                       :value="dict.value" />
          </el-select>
        </el-form-item>
        <el-form-item label="图片">
          <imageUpload v-model="form.img"/>
        </el-form-item>
        <el-form-item label="详情" prop="detail">
          <el-input v-model.trim="form.detail" placeholder="请输入详情" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listEquip, getEquip, delEquip, addEquip, updateEquip, exportEquip } from "@/api/info/equip";
import { listEquipCategory} from "@/api/info/equipCategory"
import BrandSelect from "@/views/components/BrandSelect.vue";

export default {
  name: "Equip",
  components: {BrandSelect},
  dicts: ['equop_status'],
  data() {
    return {
      equipCategory:[],
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 装备管理表格数据
      equipList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        categoryId: null,
        img: null,
        name: null,
        detail: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [
          { required: true, message: '请输入名称', trigger: 'blur' },
        ],
        categoryId: [
          { required: true, message: '请选择装备分类', trigger: 'blur' },
        ],
        brand: [
          { required: true, message: '请选择品牌', trigger: 'blur' },
        ],
        equipOptions: [
          { required: true, message: '请选择钓鱼场景', trigger: 'blur' },
        ],
        equipType: [
          { required: true, message: '请选择装备类型', trigger: 'blur' },
        ],
        img: [
          { required: true, message: '请选择图片', trigger: 'blur' },
        ],
      },
      equipOptions:[
        {value:"黑坑",label:'黑坑'},
        {value:"河流",label:'河流'},
        {value:"水库",label:'水库'},
        {value:"湖泊",label:'湖泊'},
        {value:"全部",label:'全部'},
      ],
      equipLists: [],
      columns: [
            { key: 1, label: "装备分类id", visible:  true  },
            { key: 2, label: "图片", visible:  false  },
            { key: 3, label: "名称", visible:  true  },
            { key: 4, label: "详情", visible:  true  },
             ],
    };
  },
  created() {
    this.getList();
    this.getEquipCategoryList();
  },
  methods: {
    onBrandChange(value){
      this.form.brandName = value
    },
    getName(id){
      for(var i = 0;i<this.equipCategory.length;i++){
        if(id==this.equipCategory[i].categoryId){
          return this.equipCategory[i].name;
        }
      }
    },
    getEquipCategoryList(){
      listEquipCategory().then(response=>{
        this.equipCategory = response.rows;
      })
    },
    /** 查询装备管理列表 */
    getList() {
      this.loading = true;
      const {pageNum, pageSize} = this.queryParams;
      const query = {...this.queryParams, pageNum: undefined, pageSize: undefined};
      const pageReq = {page: pageNum - 1, size: pageSize};
      listEquip(query, pageReq).then(response => {
        const { content, totalElements } = response
        this.equipList = content;
        this.total = totalElements;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        equipId: null,
        categoryId: null,
        img: null,
        name: null,
        detail: null,
        createTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.equipId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加装备管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const equipId = row.equipId || this.ids
      getEquip(equipId).then(response => {
        this.form = response;
        this.equipLists = this.form.fishingType.split(',');
        this.form.brand = parseInt(response.brand); // 转为整数
        this.open = true;
        this.title = "修改装备管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      // 将装备类型拼接成字符串
      this.form.fishingType = this.equipLists.join(',');
      console.log( "1111", this.form.fishingType)
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.equipId != null) {
            updateEquip(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addEquip(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const equipIds = row.equipId || this.ids;
      this.$modal.confirm('是否确认删除装备管理编号为"' + equipIds + '"的数据项？').then(function() {
        return delEquip(equipIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$modal.confirm('是否确认导出所有装备管理数据项？').then(() => {
        this.exportLoading = true;
        return exportEquip(queryParams);
      }).then(response => {
        this.$download.download(response);
        this.exportLoading = false;
      }).catch(() => {});
    }
  }
};
</script>
